package com.jinlejia.erp.ui.new_house.house;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.YfListRecyclerView;
import com.freedom.yefeng.yfrecyclerview.YfLoadMoreListener;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.common.WrapContentLinearLayoutManager;
import com.jinlejia.erp.ui.new_house.adapter.NewHouseAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.StringUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

public class NearbyHouseListActivity extends AppCompatActivity
        implements YfLoadMoreListener {

    NewHouseAdapter adapter;
    private int page = 1;
    private boolean mLoadingLock = false;
    private String city, county;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nearby_house_list);
        EventBus.getDefault().register(this);
        county = getIntent().getStringExtra("county");
        city = getIntent().getStringExtra("city");
        initViews();
        initData();
    }


    protected void initViews() {
        ImageView back = findViewById(R.id.id_back);
        YfListRecyclerView recyclerView = findViewById(R.id.id_nearby_list);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this));
        ArrayList<Map<String, Object>> dataList = new ArrayList<>();
        adapter = new NewHouseAdapter(dataList);
        recyclerView.setAdapter(adapter);

        back.setOnClickListener(v -> finish());
        adapter.setOnItemClickListener((view, o) -> {
            Map<String, Object> map = (Map<String, Object>) o;
            Intent intent = new Intent(NearbyHouseListActivity.this, DetailNewHouseActivity.class);
            intent.putExtra("houseCode", map.get("houseCode").toString());
            startActivity(intent);
        });

    }

    protected void initData() {
//        String url = ApiConfig.HOUSR_AROUND.concat("&pageIndex=").concat(StringUtil.valueOf(page, "1"))
//                .concat("&city=").concat(StringUtil.valueOf(city)).concat("&county=").concat(StringUtil.valueOf(city));
//        DataUtil.getInstance().getData(this, url, "TAG_NEARBY");

        OkHttpUtils.get().url(ApiConfig.CIRCLE_HOUSE_LIST)
                .addParams("pageIndex", StringUtil.valueOf(page))
                .addParams("pageSize", "15")
                .addParams("city", city)
                .addParams("county", county)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        call.cancel();
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(NearbyHouseListActivity.this, response, adapter)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            if (data.containsKey("page")) {
                                JSONArray pageData = data.getJSONArray("page");
                                ArrayList<Map<String, Object>> infoList = JSON.parseObject(pageData.toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                                });
                                if (page == 1) {
                                    adapter.setData(infoList);
                                } else {
                                    if (infoList.size() > 0) {
                                        adapter.addData(infoList);
                                    } else {
                                        page--;
                                    }
                                }
                            }
                        }
                        mLoadingLock = false;
                    }
                });
    }


    @Override
    public void loadMore() {
        if (mLoadingLock) {
            return;
        }
        mLoadingLock = true;
        page++;
        adapter.addFooter("loading");
        initData();
    }
}
